package com.d0319;
public class Money {
    Object obj=new Object();
    int money=800;
    public void Table(){
        while(money>0) {
            synchronized (obj){
                Thread.currentThread().setName("A");
                System.out.println(Thread.currentThread().getName() + "在柜台取走" + 100 + "元" + " ");
                money = money - 100;
                System.out.println("剩余钱数：" + money + "元");
                try {
                    //Thread.sleep(100);
                    obj.wait(100);//将线程A进入阻塞状态
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    public void ATM(){
        while(money>0){
            synchronized (obj){
                Thread.currentThread().setName("B");
                System.out.println(Thread.currentThread().getName()+"在ATM取走"+100+"元"+" ");
                money=money-100;
                System.out.println("剩余钱数：" + money + "元");
                try {
                    //Thread.sleep(100);
                    obj.wait(100);//将线程B进入阻塞状态
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    public void save() {
            synchronized (obj) {
                Thread.currentThread().setName("C用户");
                System.out.println(Thread.currentThread().getName() + "存入" + 2000 + "元" + " ");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                money = money + 2000;
                System.out.println("剩余钱数:" + money + "元");
            }
        }


    public static void main(String[] args) {
        Money mon=new Money();
        new Thread(()->mon.ATM()).start();
        new Thread(()->mon.Table()).start();
        new Thread(()->mon.save()).start();

    }
}